home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / src / Windows / wgl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-04  |  10.7 KB  |  407 lines

  1. /*
  2. * This library is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU Library General Public
  4. * License as published by the Free Software Foundation; either
  5. * version 2 of the License, or (at your option) any later version.
  6. *
  7. * This library is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  10. * Library General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU Library General Public
  13. * License along with this library; if not, write to the Free
  14. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. *
  16. */
  17.  
  18. /*
  19. * File name     : wgl.c
  20. * WGL stuff. Added by Oleg Letsinsky, ajl@ultersys.ru
  21. * Some things originated from the 3Dfx WGL functions
  22. */
  23.  
  24.  
  25. #ifdef WIN32
  26.  
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30.  
  31. #include <GL/gl.h>
  32. #include <GL/glu.h>
  33.  
  34. #ifdef __cplusplus
  35. }
  36. #endif
  37.  
  38. #include <stdio.h>
  39. #include <windows.h>
  40. #include <tchar.h>
  41. #include "wmesadef.h"
  42. #include "GL/wmesa.h"
  43. #include "types.h"
  44.  
  45. #define MAX_MESA_ATTRS    20
  46.  
  47. struct __extensions__
  48. {
  49.     PROC    proc;
  50.     char    *name;
  51. };
  52.  
  53. struct __pixelformat__
  54. {
  55.     PIXELFORMATDESCRIPTOR    pfd;
  56.     GLboolean doubleBuffered;
  57. };
  58.  
  59. struct __extensions__    ext[] = {
  60.  
  61. #ifdef GL_EXT_polygon_offset
  62.     {    (PROC)glPolygonOffsetEXT,        "glPolygonOffsetEXT"            },
  63. #endif
  64.     {    (PROC)glBlendEquationEXT,        "glBlendEquationEXT"            },
  65.     {    (PROC)glBlendColorEXT,            "glBlendColorExt"            },
  66.     {    (PROC)glVertexPointerEXT,        "glVertexPointerEXT"            },
  67.     {    (PROC)glNormalPointerEXT,        "glNormalPointerEXT"            },
  68.     {    (PROC)glColorPointerEXT,        "glColorPointerEXT"            },
  69.     {    (PROC)glIndexPointerEXT,        "glIndexPointerEXT"            },
  70.     {    (PROC)glTexCoordPointerEXT,        "glTexCoordPointer"            },
  71.     {    (PROC)glEdgeFlagPointerEXT,        "glEdgeFlagPointerEXT"                },
  72.     {    (PROC)glGetPointervEXT,            "glGetPointervEXT"            },
  73.     {    (PROC)glArrayElementEXT,        "glArrayElementEXT"            },
  74.     {    (PROC)glDrawArraysEXT,            "glDrawArrayEXT"            },
  75.     {    (PROC)glAreTexturesResidentEXT,    "glAreTexturesResidentEXT"                    },
  76.     {    (PROC)glBindTextureEXT,            "glBindTextureEXT"            },
  77.     {    (PROC)glDeleteTexturesEXT,        "glDeleteTexturesEXT"            },
  78.     {    (PROC)glGenTexturesEXT,            "glGenTexturesEXT"            },
  79.     {    (PROC)glIsTextureEXT,            "glIsTextureEXT"            },
  80.     {    (PROC)glPrioritizeTexturesEXT,    "glPrioritizeTexturesEXT"                },
  81.     {    (PROC)glCopyTexSubImage3DEXT,    "glCopyTexSubImage3DEXT"                },
  82.     {    (PROC)glTexImage3DEXT,            "glTexImage3DEXT"            },
  83.     {    (PROC)glTexSubImage3DEXT,        "glTexSubImage3DEXT"            },
  84. };
  85.  
  86. int                qt_ext = sizeof(ext) / sizeof(ext[0]);
  87.  
  88. struct __pixelformat__    pix[] =
  89. {
  90.     {    {    sizeof(PIXELFORMATDESCRIPTOR),    1,
  91.         PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_GENERIC_FORMAT|PFD_DOUBLEBUFFER|PFD_SWAP_COPY,
  92.         PFD_TYPE_RGBA,
  93.         24,    8,    0,    8,    8,    8,    16,    8,    24,
  94.         0,    0,    0,    0,    0,    16,    8,    0,    0,    0,    0,    0,    0 },
  95.         GL_TRUE
  96.     },
  97.     {    {    sizeof(PIXELFORMATDESCRIPTOR),    1,
  98.         PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_GENERIC_FORMAT,
  99.         PFD_TYPE_RGBA,
  100.         24,    8,    0,    8,    8,    8,    16,    8,    24,
  101.         0,    0,    0,    0,    0,    16,    8,    0,    0,    0,    0,    0,    0 },
  102.         GL_FALSE
  103.     },
  104. };
  105.  
  106. int                qt_pix = sizeof(pix) / sizeof(pix[0]);
  107.  
  108. typedef struct {
  109.     WMesaContext ctx;
  110.     HDC hdc;
  111. } MesaWglCtx;
  112.  
  113. #define MESAWGL_CTX_MAX_COUNT 20
  114.  
  115. static MesaWglCtx wgl_ctx[MESAWGL_CTX_MAX_COUNT];
  116.  
  117. static unsigned ctx_count = 0;
  118. static unsigned ctx_current = 0;
  119. static unsigned curPFD = 0;
  120.  
  121. WINGDIAPI BOOL WINAPI wglCopyContext(HGLRC hglrcSrc,HGLRC hglrcDst,UINT mask)
  122. {
  123.     return(FALSE);
  124. }
  125.  
  126. WINGDIAPI HGLRC WINAPI wglCreateContext(HDC hdc)
  127. {
  128.     HWND        hWnd;
  129.     int i = 0;
  130.     if(!(hWnd = WindowFromDC(hdc)))
  131.     {
  132.         SetLastError(0);
  133.         return(NULL);
  134.     }
  135.     if (!ctx_count)
  136.     {
  137.         for(i=0;i<MESAWGL_CTX_MAX_COUNT;i++)
  138.         {
  139.             wgl_ctx[i].ctx = NULL;
  140.             wgl_ctx[i].hdc = NULL;
  141.         }
  142.     }
  143.     for( i = 0; i < MESAWGL_CTX_MAX_COUNT; i++ )
  144.     {
  145.         if ( wgl_ctx[i].ctx == NULL )
  146.         {
  147.             wgl_ctx[i].ctx = WMesaCreateContext( hWnd, NULL, GL_TRUE,
  148.                 pix[curPFD-1].doubleBuffered );
  149.             if (wgl_ctx[i].ctx == NULL)
  150.                 break;
  151.             wgl_ctx[i].hdc = hdc;
  152.             ctx_count++;
  153.             return ((HGLRC)wgl_ctx[i].ctx);
  154.         }
  155.     }
  156.     SetLastError(0);
  157.     return(NULL);
  158. }
  159.  
  160. WINGDIAPI BOOL WINAPI wglDeleteContext(HGLRC hglrc)
  161. {
  162.     int i;
  163.     for ( i = 0; i < MESAWGL_CTX_MAX_COUNT; i++ )
  164.     {
  165.         if ( wgl_ctx[i].ctx == hglrc )
  166.         {
  167.             WMesaMakeCurrent(hglrc);
  168.             WMesaDestroyContext();
  169.             wgl_ctx[i].ctx = NULL;
  170.             wgl_ctx[i].hdc = NULL;
  171.             ctx_count--;
  172.             return(TRUE);
  173.         }
  174.     }
  175.     SetLastError(0);
  176.     return(FALSE);
  177. }
  178.  
  179. WINGDIAPI HGLRC WINAPI wglCreateLayerContext(HDC hdc,int iLayerPlane)
  180. {
  181.     SetLastError(0);
  182.     return(NULL);
  183. }
  184.  
  185. WINGDIAPI HGLRC WINAPI wglGetCurrentContext(VOID)
  186. {
  187.     return wgl_ctx[ctx_current].ctx;
  188. }
  189.  
  190. WINGDIAPI HDC WINAPI wglGetCurrentDC(VOID)
  191. {
  192.     return wgl_ctx[ctx_current].hdc;
  193. }
  194.  
  195. WINGDIAPI BOOL WINAPI wglMakeCurrent(HDC hdc,HGLRC hglrc)
  196. {
  197.     int i;
  198.     for ( i = 0; i < MESAWGL_CTX_MAX_COUNT; i++ )
  199.     {
  200.         if ( wgl_ctx[i].ctx == hglrc )
  201.         {
  202.             wgl_ctx[i].hdc = hdc;
  203.             WMesaMakeCurrent( (WMesaContext) hglrc );
  204.             ctx_current = i;
  205.             return TRUE;
  206.         }
  207.     }
  208.     return FALSE;
  209. }
  210.  
  211. WINGDIAPI BOOL WINAPI wglShareLists(HGLRC hglrc1,HGLRC hglrc2)
  212. {
  213.     return(TRUE);
  214. }
  215.  
  216. WINGDIAPI BOOL WINAPI wglUseFontBitmapsA(HDC hdc,DWORD first,DWORD count,DWORD listBase)
  217. {
  218.     return(FALSE);
  219. }
  220.  
  221. WINGDIAPI BOOL WINAPI wglUseFontBitmapsW(HDC hdc,DWORD first,DWORD count,DWORD listBase)
  222. {
  223.     return(FALSE);
  224. }
  225.  
  226. WINGDIAPI BOOL WINAPI wglUseFontOutlinesA(HDC hdc,DWORD first,DWORD count,
  227.                                   DWORD listBase,FLOAT deviation,
  228.                                   FLOAT extrusion,int format,
  229.                                   LPGLYPHMETRICSFLOAT lpgmf)
  230. {
  231.     SetLastError(0);
  232.     return(FALSE);
  233. }
  234.  
  235. WINGDIAPI BOOL WINAPI wglUseFontOutlinesW(HDC hdc,DWORD first,DWORD count,
  236.                                   DWORD listBase,FLOAT deviation,
  237.                                   FLOAT extrusion,int format,
  238.                                   LPGLYPHMETRICSFLOAT lpgmf)
  239. {
  240.     SetLastError(0);
  241.     return(FALSE);
  242. }
  243.  
  244. WINGDIAPI BOOL WINAPI wglDescribeLayerPlane(HDC hdc,int iPixelFormat,
  245.                                     int iLayerPlane,UINT nBytes,
  246.                                     LPLAYERPLANEDESCRIPTOR plpd)
  247. {
  248.     SetLastError(0);
  249.     return(FALSE);
  250. }
  251.  
  252. WINGDIAPI int WINAPI wglSetLayerPaletteEntries(HDC hdc,int iLayerPlane,
  253.                                        int iStart,int cEntries,
  254.                                        CONST COLORREF *pcr)
  255. {
  256.     SetLastError(0);
  257.     return(0);
  258. }
  259.  
  260. WINGDIAPI int WINAPI wglGetLayerPaletteEntries(HDC hdc,int iLayerPlane,
  261.                                        int iStart,int cEntries,
  262.                                        COLORREF *pcr)
  263. {
  264.     SetLastError(0);
  265.     return(0);
  266. }
  267.  
  268. WINGDIAPI BOOL WINAPI wglRealizeLayerPalette(HDC hdc,int iLayerPlane,BOOL bRealize)
  269. {
  270.     SetLastError(0);
  271.     return(FALSE);
  272. }
  273.  
  274. WINGDIAPI BOOL WINAPI wglSwapLayerBuffers(HDC hdc,UINT fuPlanes)
  275. {
  276.     if( !hdc )
  277.     {
  278.         WMesaSwapBuffers();
  279.         return(TRUE);
  280.     }
  281.     SetLastError(0);
  282.     return(FALSE);
  283. }
  284.  
  285. WINGDIAPI int WINAPI wglChoosePixelFormat(HDC hdc,
  286.                                   CONST PIXELFORMATDESCRIPTOR *ppfd)
  287. {
  288.     int        i,best = -1,bestdelta = 0x7FFFFFFF,delta,qt_valid_pix;
  289.  
  290.     qt_valid_pix = qt_pix;
  291.     if(ppfd->nSize != sizeof(PIXELFORMATDESCRIPTOR) || ppfd->nVersion != 1)
  292.     {
  293.         SetLastError(0);
  294.         return(0);
  295.     }
  296.     for(i = 0;i < qt_valid_pix;i++)
  297.     {
  298.         delta = 0;
  299.         if(
  300.             (ppfd->dwFlags & PFD_DRAW_TO_WINDOW) &&
  301.             !(pix[i].pfd.dwFlags & PFD_DRAW_TO_WINDOW))
  302.             continue;
  303.         if(
  304.             (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) &&
  305.             !(pix[i].pfd.dwFlags & PFD_DRAW_TO_BITMAP))
  306.             continue;
  307.         if(
  308.             (ppfd->dwFlags & PFD_SUPPORT_GDI) &&
  309.             !(pix[i].pfd.dwFlags & PFD_SUPPORT_GDI))
  310.             continue;
  311.         if(
  312.             (ppfd->dwFlags & PFD_SUPPORT_OPENGL) &&
  313.             !(pix[i].pfd.dwFlags & PFD_SUPPORT_OPENGL))
  314.             continue;
  315.         if(
  316.             !(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) &&
  317.             ((ppfd->dwFlags & PFD_DOUBLEBUFFER) != (pix[i].pfd.dwFlags & PFD_DOUBLEBUFFER)))
  318.             continue;
  319.         if(
  320.             !(ppfd->dwFlags & PFD_STEREO_DONTCARE) &&
  321.             ((ppfd->dwFlags & PFD_STEREO) != (pix[i].pfd.dwFlags & PFD_STEREO)))
  322.             continue;
  323.         if(ppfd->iPixelType != pix[i].pfd.iPixelType)
  324.             delta++;
  325.         if(delta < bestdelta)
  326.         {
  327.             best = i + 1;
  328.             bestdelta = delta;
  329.             if(bestdelta == 0)
  330.                 break;
  331.         }
  332.     }
  333.     if(best == -1)
  334.     {
  335.         SetLastError(0);
  336.         return(0);
  337.     }
  338.     return(best);
  339. }
  340.  
  341. WINGDIAPI int WINAPI wglDescribePixelFormat(HDC hdc,int iPixelFormat,UINT nBytes,
  342.                                     LPPIXELFORMATDESCRIPTOR ppfd)
  343. {
  344.     int        qt_valid_pix;
  345.  
  346.     qt_valid_pix = qt_pix;
  347.     if(iPixelFormat < 1 || iPixelFormat > qt_valid_pix || nBytes != sizeof(PIXELFORMATDESCRIPTOR))
  348.     {
  349.         SetLastError(0);
  350.         return(0);
  351.     }
  352.     *ppfd = pix[iPixelFormat - 1].pfd;
  353.     return(qt_valid_pix);
  354. }
  355.  
  356. /*
  357. * GetProcAddress - return the address of an appropriate extension
  358. */
  359. WINGDIAPI PROC WINAPI wglGetProcAddress(LPCSTR lpszProc)
  360. {
  361.     int        i;
  362.     for(i = 0;i < qt_ext;i++)
  363.         if(!strcmp(lpszProc,ext[i].name))
  364.             return(ext[i].proc);
  365.  
  366.         SetLastError(0);
  367.         return(NULL);
  368. }
  369.  
  370. WINGDIAPI int WINAPI wglGetPixelFormat(HDC hdc)
  371. {
  372.     if(curPFD == 0)
  373.     {
  374.         SetLastError(0);
  375.         return(0);
  376.     }
  377.     return(curPFD);
  378. }
  379.  
  380. WINGDIAPI BOOL WINAPI wglSetPixelFormat(HDC hdc,int iPixelFormat,
  381.                                 PIXELFORMATDESCRIPTOR *ppfd)
  382. {
  383.     int        qt_valid_pix;
  384.  
  385.     qt_valid_pix = qt_pix;
  386.     if(iPixelFormat < 1 || iPixelFormat > qt_valid_pix || ppfd->nSize != sizeof(PIXELFORMATDESCRIPTOR))
  387.     {
  388.         SetLastError(0);
  389.         return(FALSE);
  390.     }
  391.     curPFD = iPixelFormat;
  392.     return(TRUE);
  393. }
  394.  
  395. WINGDIAPI BOOL WINAPI wglSwapBuffers(HDC hdc)
  396. {
  397.     if(wgl_ctx[ctx_current].ctx==NULL)
  398.     {
  399.         SetLastError(0);
  400.         return(FALSE);
  401.     }
  402.     WMesaSwapBuffers();
  403.     return(TRUE);
  404. }
  405.  
  406. #endif /* WIN32 */
  407.